package com.kang;

import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GifOperator {

    public static void main(String[] args) throws IOException {

        String outputPath = "D:\\image/s01.gif";
        String imagePath = "D:\\image/duck.gif";
        reverseGif(imagePath,outputPath);
        // Gif转图片
        String dirPath = "D:/image/";
        int size = gifToImages(imagePath,dirPath);
        List<BufferedImage> images = new ArrayList<>();
        for (int i = 0 ; i < size;i++) {
            File outFile = new File(dirPath + i + ".png");
            BufferedImage image = ImageIO.read(outFile);
            Graphics2D graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(Color.black);
            graphics.setFont(new Font("黑体", Font.BOLD, 20));
            switch (i) {
                case 0: graphics.drawString("我是",45, 107);break;//0,45,77
                case 1: graphics.drawString("我是",41, 95);break;//1,41,65
                case 2: graphics.drawString("我是",36, 76);break;//2,36,46
                case 3: break;//3,空
                case 4: graphics.drawString("可达鸭",322, 66);break;//4,322,36
                case 5: graphics.drawString("可达鸭",326, 79);break;//5,326,49
                case 6: graphics.drawString("可达鸭",326, 72);break;//6,326,42
                case 7: graphics.drawString("可达鸭",326, 75);break;//7,326,45
                case 8: graphics.drawString("可达鸭",331, 75);break;//8,331,45
                case 9: graphics.drawString("可达鸭",328, 76);break;//9,328,46
                case 10: graphics.drawString("可达鸭",325, 67);break;//10,325,37
                case 11: graphics.drawString("可达鸭",328, 70);break;//11,328,40\
                case 12: break;//12,空
                case 13: graphics.drawString("我是",57, 93);break;//13,57,63
                case 14: graphics.drawString("我是",53, 85);break;//14,53,55
                case 15: graphics.drawString("我是",69, 98);break;//15,69,68
                case 16: graphics.drawString("我是",62, 90);break;//16,62,60
                case 17: graphics.drawString("我是",56, 85);break;//17,56,55
                default: break;
            }
            images.add(image);
        }
        imagesToGif(images,"D:\\image\\res.gif");
    }

    /**
     * 多图片转gif
     * @param imageList
     * @param outputPath
     * @throws IOException
     */
    public static void imagesToGif(List<BufferedImage> imageList, String outputPath) throws IOException {
        // 拆分一帧一帧的压缩之后合成
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start(outputPath);
        encoder.setRepeat(0);
        for (BufferedImage bufferedImage : imageList) {
            //每帧之间的间隔时间
            encoder.setDelay(200);
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            BufferedImage zoomImage = new BufferedImage(width, height, 3);
            Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            Graphics gc = zoomImage.getGraphics();
            gc.setColor(Color.WHITE);
            gc.drawImage(image, 0, 0, null);
            encoder.addFrame(zoomImage);
        }
        encoder.finish();
        File outFile = new File(outputPath);
        BufferedImage image = ImageIO.read(outFile);
        ImageIO.write(image, outFile.getName(), outFile);
    }

    /**
     * Gif转图片集
     * @param imagePath
     * @param outputDirPath
     * @throws IOException
     */
    static int gifToImages(String imagePath,String outputDirPath) throws IOException {
        GifDecoder decoder = new GifDecoder();
        int status = decoder.read(imagePath);
        if (status != GifDecoder.STATUS_OK) {
            throw new IOException("read image " + imagePath + " error!");
        }
        for (int i = 0; i < decoder.getFrameCount();i++) {
            BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流
            File outFile = new File(outputDirPath + i + ".png");
            ImageIO.write(bufferedImage, "png", outFile);
        }
        return decoder.getFrameCount();
    }

    /**
     * 视频倒放
     * @param imagePath
     * @param outputPath
     * @throws IOException
     */
    public static void reverseGif(String imagePath,String outputPath) throws IOException {
        GifDecoder decoder = new GifDecoder();
        int status = decoder.read(imagePath);
        if (status != GifDecoder.STATUS_OK) {
            throw new IOException("read image " + imagePath + " error!");
        }
        // 拆分一帧一帧的压缩之后合成
        AnimatedGifEncoder encoder = new AnimatedGifEncoder();
        encoder.start(outputPath);
        encoder.setRepeat(decoder.getLoopCount());
        for (int i = decoder.getFrameCount() -1; i >= 0; i--) {
            encoder.setDelay(decoder.getDelay(i));// 设置播放延迟时间
            BufferedImage bufferedImage = decoder.getFrame(i);// 获取每帧BufferedImage流
            int height = bufferedImage.getHeight();
            int width = bufferedImage.getWidth();
            BufferedImage zoomImage = new BufferedImage(width, height, bufferedImage.getType());
            Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            Graphics gc = zoomImage.getGraphics();
            gc.setColor(Color.WHITE);
            gc.drawImage(image, 0, 0, null);
            encoder.addFrame(zoomImage);
        }
        encoder.finish();
        File outFile = new File(outputPath);
        BufferedImage image = ImageIO.read(outFile);
        ImageIO.write(image, outFile.getName(), outFile);
    }

}
